<?php

namespace teach\service;

use Yii;

class Payment
{
    /**
     * 查询一条拼团产品的详细信息
     * @param $where
     * @param $andWhere
     * @return array|false
     */
    public function assembleDetail($where, $andWhere)
    {
        $time = time();
        $sql = "SELECT s.product_no, s.stock, s.alone_price, s.assemble_price, p.id, p.freight, p.mid, p.label, p.limited, p.effective_time FROM mall_assemble_specs AS s LEFT JOIN mall_assemble_product AS p ON p.id = s.pid WHERE s.id = {$where} AND p.id = {$andWhere} AND p.status = 1 AND p.start_time < {$time} AND p.end_time > {$time}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 获取下单用户
     * @param $map
     * @return array|false
     */
    public function assembleUser($map)
    {
        $sql = "SELECT avatar_url, nickname FROM edu_user WHERE {$map}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 插入拼团订单
     * @param $order
     * @return int
     */
    public function orderInsert($order)
    {
        return Yii::$app->db->createCommand()->insert('mall_assemble_order', $order)->execute();
    }

    /**
     * 查找订单id
     * @param $out_trade_no
     * @return array|false
     */
    public function orderGetId($out_trade_no)
    {
        $sql = "SELECT id, out_trade_no FROM mall_assemble_order WHERE out_trade_no = '{$out_trade_no}'";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 减去拼单产品的库存
     * @param $sku_id
     * @param $count
     * @return int
     */
    public function productReduce($sku_id, $count)
    {
        $sql = "UPDATE mall_assemble_specs SET stock = stock - {$count} WHERE id = {$sku_id}";
        return Yii::$app->db->createCommand($sql)->execute();
    }

}